import { OnInit, OnDestroy } from '@angular/core';

import { Util } from '../../utils/util';

import { Subscription } from 'rxjs';

import {ToastyService} from '../../modules/ng2-toasty';
export class ProductBase implements OnInit {

  user:any;

  // busy
  busy:Subscription;


// http service
  service:any;


  constructor(Service,public util:Util,public toastyService :ToastyService) {

    this.user = this.util.sessionStore().get('user');

    this.util.sortHandel(this.param);

    this.service = Service;
  }


  // 实现数据绑定
  shopType:string = "";

  vm:any ={
    page: 1,
    table:{
      products:[],
      count:0
    }
  };

  // 参数
  param:any = {
    start:0,
    rows:10,
    sort: 'sellMoney',
    sortType:'desc'
  };

  // 列表
  public columns:Array<any> = [
  {
    title: '商品',
    name: 'brandName',
    sort: false,
    class:'productTh'
  },
  {
    title: '标题',
    name: 'categoryName',
    sort: false,
    class:'titleTh'
  },
  {
    title: '品牌',
    name: 'shopNum',
    sort: false
  },
  {
    title: '类目',
    name: 'productNum',
    sort: false
  },
  {
    title: '日销售额',
    name: 'sellMoney',
    sort: true,
    class: "text-overflow"
  },
  {
    title: '日销售量',
    name: 'sellAmount',
    sort: true,
    class: "text-overflow"
  },
  {
    title: '价格',
    name: 'price',
    sort: true,
    class: "text-overflow"
  },
  {
    title: '近30天销售额',
    name: 'monthSellMoney',
    sort: true,
    class: "text-overflow"
  },
  {
    title: '近30天销售量',
    name: 'monthSellAmount',
    sort: true,
    class: "text-overflow"
  },
  {
    title: '近30天成交均价',
    name: 'monthPrice',
    sort: true,
    class: "text-overflow"
  }
  ];


  ngOnInit() {


  }

  ngOnDestroy(){

    if(this.exportBusy){
      this.exportBusy.unsubscribe();
    }
    if(this.busy){
      this.busy.unsubscribe();
    }
  }


  catStore:any;
  // out handel
  handleUpdated(cat){
    console.log('handel');
    console.log(this.catStore);
    console.log(cat);
    if(! this.util.equals(this.catStore,cat)){

      this.catStore = Object.assign({},cat);

      this.param.catName = cat.wholeName;

      this.param.categoryId = cat.categoryId;

      console.log('cat $watch');

      console.log(cat);

      this.getHttp(this.param);
    }

  }



  sortChanged(sort){
    console.log('xxx');
    console.log(sort);
    this.getHttp(this.param);
  }
  //分页
  pageChanged(currentPage){
    console.log(currentPage);


    this.vm.page = currentPage;

    this.param.start = (currentPage-1)*this.param.rows;

    this.getHttp(this.param);

  }


  getHttp(param){

    this.busy = this.service.getProduct(param).subscribe((res:any)=>{

      this.vm.table.products = res.data.value;

      this.vm.table.count = res.data.count;

    },(err:any)=>{

    });
  }




  exportBusy:any;
  productExport(param,dim,dimText,fileName){

    let json = Object.assign({},param);

    json.rows = 1000;
    json.start = 0;

    // toast
    let toastOptions = {
      title: "",
      msg: "下载中...",
      showClose: true,
      timeout: 6000,
      theme: 'material'
    };
    // Add see all possible types in one shot
    this.toastyService.info(toastOptions);

    this.exportBusy = this.service.getProduct(json).subscribe((res: any) => {


      let data = this.util.jsonToArrry(res.data.value, dim,  dimText);

      this.util.export(data, fileName);


    }, (err: any) => {
      this.exportBusy.unsubscribe();
    });
  }

}
